
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace eCom.Gui
{
	public class eComSalesOrderMngtBizProc : BaseBusinessProcess
	{
		public eComSalesOrderMngtBizProc(ActiveBizProc procState) : base(procState) {  }

		
		protected override void setupRuleNames()
		{
			m_getBizObjListRule= "getSalesOrders";
			m_getBizObjDetailsRule= "getSalesOrderDetails";
			m_updateBizObjRule = "updateSalesOrder";
			m_deleteBizObjRule = "deleteSalesOrder";
		}
		
		public override IBizObj createNewBizObj()
		{
			IBizObj bizobj = App.bizObjMgr.create("eComSalesOrder");
			bizobj.put("id", CurrentOrderID);
			bizobj.put("customer",CurrentUser);
			bizobj.put("createdDate", DateTime.Now.ToUniversalTime());
			return bizobj;
		}

		protected override object[] getDetailsBizRuleParameters(object keyValue)
		{
			if(keyValue is string)
				return new object[]{new Swaf.BGuid.Guid((string)keyValue)};
			return base.getDetailsBizRuleParameters(keyValue);
		}
		public override void updateActionState()
		{
			base.updateActionState();
		}
		protected bool stillOpen()
		{
			IBizObj b = CurrentBizObj;
			return b.get("status.id").Equals(NewOrderStatusId);
		}
		protected Swaf.BGuid.Guid CurrentOrderID
		{
			get
			{
				bool showLastId = false;
				if (m_procState.get("showLastOrder") != null)
					showLastId = Convert.ToBoolean(m_procState.get("showLastOrder"));

				if (true.Equals(showLastId) && App.sessionData["LastEComOrderId"] is Swaf.BGuid.Guid)
					return App.sessionData["LastEComOrderId"] as Swaf.BGuid.Guid;

				return App.ruleMgr.execute("getCurrentSalesOrderId").CallResults as Swaf.BGuid.Guid;
			}
			//set { App.sessionData["CurrentOrderId"] = value; }
		}

		protected Swaf.BGuid.Guid NewOrderStatusId
		{
			get
			{
				if (App.sessionData["eComSysNewOrderStatusId"] == null)
				{
					IResults rc = App.ruleMgr.execute("getArticleDetail", "ecom_sysStatus_Open", false, DetailLevel.idOnly, false, true, null);
					if (rc.ExceptionInfo != null || rc.CallResults == null)
						displayMessageFromBizRuleException("Error-CannotFindOrderStatus", rc.ExceptionInfo, "ecom_sysStatus_Open");
					App.sessionData["eComSysNewOrderStatusId"] = ((IBizObj)rc.CallResults).get("id");
				}
				return App.sessionData["eComSysNewOrderStatusId"] as Swaf.BGuid.Guid;
			}
		}
		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("DeleteBizObj", true, deleteBizObj, this));
		}
		public override Privileges CurrentEditPrivilege
		{
			//TODO: this is true only if this is that user's order, other wise it should be based on the "manage sales orders" privege
			get { return CurrentPrincipal.getPrivilege("AppFunctions.ManageProducts"); }
		}
		
		protected virtual IBizObj calcCriteria(object parentId)
		{
			IBizObj options = App.bizObjMgr.create("FilterSortOptions");
			IBizObj criteria = ((IBizObjList)options.get("filterOptions")).insertNew();
			criteria.put("field", "companyId");
			criteria.put("criteria", "e");
			criteria.put("value", CurrentCompanyId);
			return options;
		}

		//This method is needed because validate bizobj goes deep into all fields of the bizobj, and because sales order
		//has so many other business objects within it, it doesnt make sense to do a full validation and get errors back.
		//We do use the validateBizObj, because that also converts field values to their properly type, such as a string
		//being put into a guid field - valudate will turn the string into a guid if it can.  Edits to bizobjs via displays
		//and grids just stick the text as it was keyed in, into the bizobj.
		protected override bool validateBizObj(IBizObj obj)
		{
			base.validateBizObj(obj, false);
			return true;
		}

		public override void updateBizObj()
		{
			base.updateBizObj();
			getCurrentBizObj();
		}
	}
}
